package com.gupao.nio;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class NoBlockingServer {

    public static List<SocketChannel> channelList = new ArrayList<>();

    public static void main(String[] args) {

        try {
            // 相当于serverSocket
            // 1.支持非阻塞  2.数据总是写入buffer,读取也是从buffer中去读  3.可以同时读写
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.socket().bind(new InetSocketAddress(8080));
            while (true){
                // 这里将不再阻塞
                SocketChannel socketChannel = serverSocketChannel.accept();

                if(socketChannel != null){
                    socketChannel.configureBlocking(false);
                    channelList.add(socketChannel);
                }else {
                    System.out.println("没有请求过来！！！");
                }
                for (SocketChannel client : channelList){
                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    // 也不阻塞
                    int num = client.read(byteBuffer);
                    if(num>0){
                        System.out.println("客户端端口："+ client.socket().getPort()+",客户端收据："+new String(byteBuffer.array()));
                    }else {
                        System.out.println("等待客户端写数据");
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
